package listener;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebListener
public class UserCountListener implements HttpSessionListener{

    public UserCountListener() {
    }



    @Override
    public void sessionCreated(HttpSessionEvent se) {
        /*Session创建出来时执行  Session is created. */
        ServletContext ctx = se.getSession().getServletContext();
        Integer count = (Integer) ctx.getAttribute("OnlineCount");
        if (count ==null){
            count = 0;
        }
        count ++;
        ctx.setAttribute("OnlineCount",count);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        /* Session销毁出来时执行Session is destroyed. */
        ServletContext ctx = se.getSession().getServletContext();
        Integer count = (Integer) ctx.getAttribute("OnlineCount");
        if (count ==null){
            count = 0;
        }
        count ++;
        ctx.setAttribute("OnlineCount",count);
    }


}
